﻿
Public Class ActivCatg
    Dim db As New Connexion
    Dim action As String
    Dim Entrer As String
    Dim TextBoxID As String
    Public databaseName As String
    Private Sub ActivCatg_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()

    End Sub
    Private Sub Typologie_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Entrer = ""
        DataGridViewTypol.MultiSelect = False
        DataGridViewTypol.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridViewTypol.ReadOnly = True
        miseajour()
    End Sub

    Private Sub ButtonAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.TextBoxCodeTypol.Text = ""
        Me.TextBoxNomTypol.Text = ""
        Me.TextBoxDef.Text = ""
        action = "ajouter"
    End Sub


    Private Sub ButtonEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click
        Entrer = ""
        If action = "ajouter" Then
            db.myReq = "INSERT INTO  [dbo].[ActvCateg]([Cod_EquipCateg2], [Nom_ActvCateg],[Exp_ActvCateg])VALUES('" & Me.TextBoxCodeTypol.Text & "','" & Me.TextBoxNomTypol.Text & "','" & Me.TextBoxDef.Text & "')"
        Else
            db.myReq = "Update  [dbo].[ActvCateg] SET [Cod_EquipCateg2] = '" & Me.TextBoxCodeTypol.Text & "', [Nom_ActvCateg] = '" & Me.TextBoxNomTypol.Text & "',[Exp_ActvCateg] = '" & Me.TextBoxDef.Text & "' WHERE [Id_ActvCateg2]='" & TextBoxID & "'"
        End If
        db.Connecter(databaseName)
        db.Disconnecter()
        miseajour()

    End Sub

    Private Sub miseajour()
        db.myReq = "SELECT * FROM  [dbo].[ActvCateg]"
        db.Connecter(databaseName)
        DataGridViewTypol.Rows.Clear()
        While db.rdr.Read()
            DataGridViewTypol.Rows.Add(db.rdr(0), db.rdr(1), db.rdr(2), db.rdr(3))
        End While
        db.Disconnecter()

        If DataGridViewTypol.RowCount - 1 = 0 Then
            Me.TextBoxCodeTypol.Text = ""
            Me.TextBoxNomTypol.Text = ""
            Me.TextBoxDef.Text = ""
        Else
            DataGridViewTypol.Rows(0).Selected = True
            Me.TextBoxCodeTypol.Text = DataGridViewTypol.SelectedCells(1).Value
            Me.TextBoxNomTypol.Text = DataGridViewTypol.SelectedCells(2).Value
            Me.TextBoxDef.Text = DataGridViewTypol.SelectedCells(3).Value
            TextBoxID = DataGridViewTypol.SelectedCells(0).Value
        End If
    End Sub

    Private Sub DataGridView3_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewTypol.CurrentCellChanged
        If Entrer = "debut" Then
            action = ""
            Selection()
        End If
    End Sub
    Private Sub DataGridView3_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridViewTypol.MouseClick
        If e.Button = MouseButtons.Left Then
            Entrer = "debut"
            action = ""
            Selection()
        End If
    End Sub
    Private Sub Selection()
        If DataGridViewTypol.RowCount - 1 = 0 Then
            Me.TextBoxCodeTypol.Text = ""
            Me.TextBoxNomTypol.Text = ""
            Me.TextBoxDef.Text = ""
        Else
            Me.TextBoxCodeTypol.Text = DataGridViewTypol.SelectedCells(1).Value
            Me.TextBoxNomTypol.Text = DataGridViewTypol.SelectedCells(2).Value
            Me.TextBoxDef.Text = DataGridViewTypol.SelectedCells(3).Value
            TextBoxID = DataGridViewTypol.SelectedCells(0).Value
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Dispose()
    End Sub

    Private Sub ButtonNouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouv.Click
        action = "ajouter"
        Me.TextBoxCodeTypol.Text = ""
        Me.TextBoxNomTypol.Text = ""
        Me.TextBoxDef.Text = ""
    End Sub

    Private Sub ButtonSup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSup.Click
        Entrer = ""
        action = ""
        db.myReq = "DELETE FROM  [dbo].[ActvCateg] WHERE [Id_ActvCateg2]='" & TextBoxID & "'"
        db.Connecter(databaseName)
        db.Disconnecter()
        miseajour()
    End Sub
End Class
